package myDaos;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.entities.OutletMasterGlobalTemp;


@Stateless
public class OutletMasterDao {

	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	public Boolean callOutletProcedure(String p_distributor_no){
		Boolean isCalled;
		try {
		    Query q = getEntityManager().createNativeQuery("{call NAME_HUB.CHUB_OUTLET_MASTER_REPORT_PROC(:distributor)}").setParameter("distributor",p_distributor_no);
		    q.executeUpdate();
		    isCalled=true;
	} catch (Exception e) {
		e.printStackTrace();
		isCalled=false;
	}
		return isCalled;
	}
	
	public List<OutletMasterGlobalTemp> getOutletMasterList(String distributor,String gui)
	{
		List<OutletMasterGlobalTemp> omlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append("SELECT o FROM OutletMasterGlobalTemp o WHERE 1=1"); 	
			
			if(distributor!=null && !distributor.trim().equals(""))
    	    {
				queryBiffer.append(" AND o.distributorid = :distributorid") ;	
    	    }
			
			if(gui!=null && !gui.trim().equals(""))
    	    {
				queryBiffer.append(" AND o.guino = :gui") ;	
    	    }
				
			
			System.out.println(queryBiffer.toString());
			
			Query q = getEntityManager().createQuery(queryBiffer.toString());

			if(distributor!=null && !distributor.trim().equals("")){q.setParameter("distributorid", distributor);}
			if(gui!=null && !gui.trim().equals("")){q.setParameter("gui", gui);}
			
			    omlist = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return omlist;
	}

}
